package per.hx.bs.utils;

import org.springframework.web.multipart.MultipartFile;
import per.hx.bs.entity.sys.SysFileDO;
import per.hx.bs.entity.sys.SysUserDO;
import per.hx.bs.util.FileType;
import per.hx.bs.util.FileUtil;
import per.hx.bs.config.HuXingBsConfig;
import per.hx.bs.util.SpringUtils;
import per.hx.bs.vo.MultipartFileVo;
import per.hx.bs.web.sys.service.SysFileService;
import per.hx.bs.web.sys.service.SysUserService;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/4/24  16:10
 */
public class FileUpload {

    /**
     * 修改头像
     *
     * @param multipartFileVo 图片信息
     * @return 修改后的信息
     */
    public static Map<String, Object> updatePersonalImg(MultipartFileVo multipartFileVo, Long createId) {
        Map<String, Object> result = new HashMap<String, Object>(1);

        String fileName = multipartFileVo.getFile().getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);
        SysFileDO sysFileDO = new SysFileDO(FileType.fileType(fileName), "/images/" + fileName);
        //获取图片后缀
        String prefix = fileName.substring((fileName.lastIndexOf(".") + 1));
        String[] str = multipartFileVo.getAvatarData().split(",");
        //获取截取的x坐标
        int x = (int) Math.floor(Double.parseDouble(str[0].split(":")[1]));
        //获取截取的y坐标
        int y = (int) Math.floor(Double.parseDouble(str[1].split(":")[1]));
        //获取截取的高度
        int h = (int) Math.floor(Double.parseDouble(str[2].split(":")[1]));
        //获取截取的宽度
        int w = (int) Math.floor(Double.parseDouble(str[3].split(":")[1]));
        //获取旋转的角度
        int r = Integer.parseInt(str[4].split(":")[1].replaceAll("}", ""));
        try {
            BufferedImage cutImage = ImageUtils.cutImage(multipartFileVo.getFile(), x, y, w, h, prefix);
            if (cutImage != null) {
                BufferedImage rotateImage = ImageUtils.rotateImage(cutImage, r);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                boolean flag = ImageIO.write(rotateImage, prefix, out);
                if (flag) {
                    //转换后存入数据库
                    byte[] b = out.toByteArray();
                    HuXingBsConfig huXingBsConfig = SpringUtils.getBean(HuXingBsConfig.class);
                    FileUtil.uploadFile(b, huXingBsConfig.getUploadPath(), fileName);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        SysFileService fileService = SpringUtils.getBean(SysFileService.class);
        sysFileDO.setCreateId(createId);
        Long pId = fileService.insert(sysFileDO);
        if (pId > 0) {
            SysUserDO sysUserDO = new SysUserDO();
            sysUserDO.setUserId(multipartFileVo.getUserId());
            sysUserDO.setPicId(pId);
            SysUserService userService = SpringUtils.getBean(SysUserService.class);
            if (userService.update(sysUserDO) > 0) {
                result.put("url", sysFileDO.getUrl());
            }
        }
        return result;
    }

    public static Long saveFile(MultipartFile file, Long createId) {
        // 判断文件是否为空
        if (file != null) {
            try {
                String fileName = file.getOriginalFilename();
                SysFileDO fileDO = new SysFileDO();
                fileDO.setFileName(fileName);
                fileName = FileUtil.renameToUUID(fileName);
                fileDO.setType(FileType.fileType(fileName));
                fileDO.setUrl("/files/" + fileName);
                HuXingBsConfig huXingBsConfig = SpringUtils.getBean(HuXingBsConfig.class);
                FileUtil.uploadFile(file, huXingBsConfig.getUploadPath(), fileName);
                SysFileService fileService = SpringUtils.getBean(SysFileService.class);
                fileDO.setCreateId(createId);
                return fileService.insert(fileDO);
            } catch (Exception e) {
                e.printStackTrace();
                return 0L;
            }
        }
        return 0L;
    }

}
